﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace rock.UI.common
{
    public partial class UserControlCommonListPage : UserControl
    {
        public UserControlCommonListPage()
        {
            InitializeComponent();
           
            List<ComboBoxInfo> InfoList = new List<ComboBoxInfo>();

          //  ComboBoxInfo info1 = new ComboBoxInfo() { Id = "20", Name = "1条" };
          //  InfoList.Add(info1);

            ComboBoxInfo info20 = new ComboBoxInfo() { Id = "20", Name = "20条" };
            InfoList.Add(info20);
            ComboBoxInfo info30 = new ComboBoxInfo() { Id = "30", Name = "30条" };
            InfoList.Add(info30);
            ComboBoxInfo info40 = new ComboBoxInfo() { Id = "40", Name = "40条" };
            InfoList.Add(info40);
            ComboBoxInfo info50 = new ComboBoxInfo() { Id = "50", Name = "50条" };
            InfoList.Add(info50);
            ComboBoxInfo info100 = new ComboBoxInfo() { Id = "100", Name = "100条" };
            InfoList.Add(info100);
            ComboBoxInfo info200 = new ComboBoxInfo() { Id = "200", Name = "200条" };
            InfoList.Add(info200);

            this.toolStripLabel1PageSize.ComboBox.DataSource = InfoList;
            this.toolStripLabel1PageSize.ComboBox.DisplayMember = "Name";
            this.toolStripLabel1PageSize.ComboBox.ValueMember = "Id";

            this.toolStripLabel1PageSize.ComboBox.SelectedIndex = 0;
            this.toolStripLabel1PageSize.ComboBox.SelectedIndexChanged += new System.EventHandler(this.toolStripLabel1_SelectedIndexChanged);
            
        }

        //总页数
        private int MAX_PAGE = 0;
        public int _MAX_PAGE
        {
            get
            {
                return MAX_PAGE;
            }
            set
            {
                MAX_PAGE = value;
            }
        }
        private static int MIN_PAGE_SIZE = 20;
        //分页大小
        private int PAGE_SIZE = MIN_PAGE_SIZE;
        public int _PAGE_SIZE
        {
            get
            {
                return PAGE_SIZE;
            }
            set
            {
                
                PAGE_SIZE = value;
                if (PAGE_SIZE < MIN_PAGE_SIZE)
                {
                    PAGE_SIZE = MIN_PAGE_SIZE;
                }
            }
        }
        //所有记录
        private int ALL_RECORD = 0;
        public int _ALL_RECORD
        {
            get
            {
                return ALL_RECORD;
            }
            set
            {
                ALL_RECORD = value;
            }
        }

        //当前页
        private int CURRENT_PAGE = 1;
        public int _CURRENT_PAGE
        {
            get
            {
                return CURRENT_PAGE;
            }
            set
            {
                CURRENT_PAGE = value;
            }
        }

        //向前一页
        private void toolStripButton3Previous_Click(object sender, EventArgs e)
        {
            pageClick(_CURRENT_PAGE - 1);
        }
        //向后一页
        private void toolStripButton2Next_Click(object sender, EventArgs e)
        {
            pageClick(_CURRENT_PAGE + 1);
        }
        //第一页
        private void toolStripButton5Frist_Click(object sender, EventArgs e)
        {
            pageClick(1);
        }
        //最后一页
        private void toolStripButton1Last_Click(object sender, EventArgs e)
        {
            pageClick(_MAX_PAGE);
        }
         
        
        //页码改变
        private void toolStripButton4_TextChanged(object sender, EventArgs e)
        {
         // pageClick(_CURRENT_PAGE);
        }
        //选择不同的页SIZE
        private void toolStripLabel1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Manager.getInstance().pageSize = this._PAGE_SIZE = Convert.ToInt32(this.toolStripLabel1PageSize.ComboBox.SelectedValue);

                pageClick(_CURRENT_PAGE);
            }
            catch (Exception e32 )
            {
                MessageBox.Show(e32.Message);
                
            }
            
            
        }

        //代理
        public delegate void PageClick(int  a );
        public PageClick pageClick;

        public void Inits(int allRecord, int currentPage)
        {
            this._ALL_RECORD = allRecord;
            this._CURRENT_PAGE = currentPage;
           
            if(_ALL_RECORD % _PAGE_SIZE == 0){
                this._MAX_PAGE = _ALL_RECORD / _PAGE_SIZE;
            }else{
                this._MAX_PAGE = _ALL_RECORD / _PAGE_SIZE +1 ;
            }

            if (_CURRENT_PAGE >= _MAX_PAGE)
            {
                toolStripButton2Next.Enabled = false;
            }
            else {
                toolStripButton2Next.Enabled = true;
            }

            if (_CURRENT_PAGE == _MAX_PAGE)
            {
                toolStripButton1Last.Enabled = false;
            }
            else
            {
                toolStripButton1Last.Enabled = true;
            }

            if (_CURRENT_PAGE <= 1)
            {
                toolStripButton3Previous.Enabled = false;
            }
            else {
                toolStripButton3Previous.Enabled = true;
             }


            if (_CURRENT_PAGE == 1)
            {
                toolStripButton5Frist.Enabled = false;
            }
            else
            {
                toolStripButton5Frist.Enabled = true;
            }

            this.TextBox.Text = _CURRENT_PAGE + "";
            this.toolStripLabel2.Text = "/  " + _MAX_PAGE + "";
            this.toolStripLabel3.Text = "总记录:" + allRecord + "  ";
            

        }

        

        

        

        
    }
}
